// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const voiceCollection = db.collection('voice')
const likeCollection = db.collection('liker_liked')
const savedCollection = db.collection('savedvoice')
const rightCollection = db.collection('right')
const _ = db.command
let voiceCount = 0
let likeCount = 0
let starCount = 0
let waitCount = 0
let applyCount = 0
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()

  await voiceCollection.where({
    _openid: wxContext.OPENID
  }).count().then(res => {
    voiceCount = res.total
  })

  await likeCollection.where({
    likerOpenId: wxContext.OPENID,
    likedOpenId: _.neq(wxContext.OPENID)
  }).count().then(res => {
    likeCount = res.total
  })

  await likeCollection.where({
    likerOpenId: _.neq(wxContext.OPENID),
    likedOpenId: wxContext.OPENID
  }).count().then(res => {
    starCount = res.total
  })

  await savedCollection.where({
    _openid: wxContext.OPENID
  }).count().then(res => {
    waitCount = res.total
  })

  await rightCollection.where({
    righted: wxContext.OPENID,
    status: 1
  }).count().then(res => {
    applyCount = res.total
  })

  return {
    voiceCount,
    likeCount,
    starCount,
    waitCount,
    applyCount
  }
}